package com.gin.kotlin.no_gay_alliance.store

import com.gin.kotlin.no_gay_alliance.base.NamedDataStore
import java.lang.ref.WeakReference

/**
 * 运行时数据
 * @property dataStore NamedDataStore
 * @property clearCacheDate 已执行缓存清理的日期
 * @property ignoredTagName 已忽略的APP版本tagName
 */
class RuntimeData {
    private val dataStore = NamedDataStore.getInstance(this.javaClass.simpleName)
    var clearCacheDate: String
        get() = dataStore.get(KEY_CLEAR_CACHE_DATE, "")
        set(value) {
            dataStore.set(KEY_CLEAR_CACHE_DATE, value)
        }

    var ignoredTagName: String
        get() = dataStore.get(KEY_APP_RELEASE_TAG_NAME, "")
        set(value) {
            dataStore.set(KEY_APP_RELEASE_TAG_NAME, value)
        }


    companion object {
        private const val KEY_CLEAR_CACHE_DATE = "clear_cache_date"
        private const val KEY_APP_RELEASE_TAG_NAME = "app_release_tag_name"
        private var instance: WeakReference<RuntimeData> = WeakReference(RuntimeData())

        /**
         * 单例模式+弱引用
         * @return Any
         */
        fun getInstance(): RuntimeData = instance.get() ?: RuntimeData().also { instance = WeakReference(it) }
    }
}